package ru.reaperoq.inventory.screens.main.admin.locations

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.LocationCity
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier

@Composable
fun AddLocationDialog(
    modifier: Modifier = Modifier,
    onDismissRequest: () -> Unit,
    onConfirm: (String) -> Unit
) {
    var name by remember { mutableStateOf("") }
    var error by remember(name) { mutableStateOf(false) }

    AlertDialog(
        modifier = modifier,
        onDismissRequest = onDismissRequest,
        title = {
            Text(text = "Добавить локацию")
        },
        text = {
            OutlinedTextField(
                value = name, onValueChange = {
                    name = it
                },
                singleLine = true,
                label = { Text(text = "Название") },
                modifier = Modifier.fillMaxWidth(),
                leadingIcon = {
                    Icon(imageVector = Icons.Default.LocationCity, contentDescription = null)
                },
                isError = error,
                supportingText = {
                    if (error) {
                        Text(text = "Поле не может быть пустым")
                    }
                }
            )
        },
        dismissButton = {
            TextButton(onClick = onDismissRequest) {
                Text(text = "Отмена")
            }
        },
        confirmButton = {
            Button(onClick = {
                if (name.isEmpty()) {
                    error = true
                } else {
                    onConfirm(name)
                    onDismissRequest()
                }
            }) {
                Text(text = "Добавить")
            }
        }
    )
}